/**
 * 
 */
package xiaojian.toolkit.ebus.transformer;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections4.Transformer;

/**
 * @author hp
 *
 */
public class TypeSelectTransformer implements Transformer {

	private	Map<Class<?>, Transformer>	typeRouter = 
		new HashMap<Class<?>, Transformer>();
	
	/**
	 * @return the typeRouter
	 */
	public Map<Class<?>, Transformer> getTypeRouter() {
		return Collections.unmodifiableMap(typeRouter);
	}

	/**
	 * @param typeRouter the typeRouter to set
	 */
	public void setTypeRouter(Map<Class<?>, Transformer> typeRouter) {
        this.typeRouter.clear();
        
        for (Map.Entry<Class<?>, Transformer> entry : typeRouter.entrySet()) {
            if ( null != entry.getValue() ) {
                this.typeRouter.put(entry.getKey(), entry.getValue());
            }
        }
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.collections.Transformer#transform(java.lang.Object)
	 */
	public Object transform(Object input) {
		if ( null == input ) {
			return	null;
		}
		Transformer transformer = typeRouter.get(input.getClass());
		if ( null == transformer ) {
			return	null;
		}
		return transformer.transform(input);
	}
}
